home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Ultra / UltraDemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-07  |  3.4 KB  |  141 lines  |  [TEXT/KAHL]

  1. /*    UltraDemo.c  --  Exercise the functions of the Ultra library.  Native
  2.     Floating Point not used. */
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "Ultra.h"            /* Dont't forget to include this file! */
  7.  
  8. double    total;
  9. long    PascalRow10[11];
  10. FILE     *fpout;
  11.  
  12. /*    The following procedure is an interesting, albeit weak, test of the
  13.     randomness of the bits produced by Ultra.  Here, we flip 10 coins 10 million
  14.     times to see whether we can reproduce the tenth row of Pascal's Triangle
  15.     (see UltraDemo.out, row 2 for correct answer).  The seeds below give a
  16.     perfect result, after normalization.  In a series of ten tries, I got a
  17.     perfect result four times.  This test takes about 5 minutes on an
  18.     unaccelerated MacIIci <TM>. */
  19.  
  20. void OneSimpleTest()
  21. {
  22.     double    denom;
  23.     long    i;
  24.     int        j,k;
  25.  
  26.     for (i=0;i<10000000;i++) {
  27.         k = 0;
  28.         for (j=0;j<10;j++)
  29.             k += Ultra_int1();    /* Heads or Tails? */
  30.         PascalRow10[k]++;
  31.     }
  32.     for (j=0;j<=10;j++)
  33.         fprintf(fpout," %ld",PascalRow10[j]);    /* raw counts */
  34.     fprintf(fpout,"\n\n");
  35.     denom = (PascalRow10[0] + PascalRow10[10])/2.0;
  36.     for (j=0;j<=10;j++)            /* normalize and round to nearest integer */
  37.         PascalRow10[j] = (int)(PascalRow10[j]/denom + 0.5);
  38.     for (j=0;j<=10;j++)
  39.         fprintf(fpout," %ld",PascalRow10[j]);    /* normalized row 10 */
  40.     fprintf(fpout,"\n\n");
  41. }
  42.  
  43. void Exercise(int run)            /* runtime = about 15 seconds */
  44. {
  45.     double    total;
  46.     float    mean,sigma;
  47.     long    i,calls[16];
  48.     int        k;
  49.     
  50.     total = 0.0;
  51.     for (k=0;k<16;k++)
  52.         calls[k] = 0;
  53.     for (i=0;i<500000;i++) {
  54.         k = Ultra_int7() & 15;
  55.         calls[k]++;
  56.         switch (k) {
  57.             case 0:
  58.                 total += (double)Ultra_long32();
  59.                 break;
  60.             case 1:
  61.                 total += (double)Ultra_long31();
  62.                 break;
  63.             case 2:
  64.                 total -= (double)Ultra_long31();
  65.                 break;
  66.             case 3:
  67.                 total += (double)Ultra_int16();
  68.                 break;
  69.             case 4:
  70.                 total += (double)Ultra_int15();
  71.                 break;
  72.             case 5:
  73.                 total -= (double)Ultra_int15();
  74.                 break;
  75.             case 6:
  76.                 total += (double)Ultra_int8();
  77.                 break;
  78.             case 7:
  79.                 total += (double)Ultra_int8u();
  80.                 break;
  81.             case 8:
  82.                 total += (double)Ultra_int7();
  83.                 break;
  84.             case 9:
  85.                 total += (double)Ultra_int1();
  86.                 break;
  87.             case 10:
  88.                 total += (double)Ultra_uni();
  89.                 break;
  90.             case 11:
  91.                 total += (double)Ultra_vni();
  92.                 break;
  93.             case 12:
  94.                 total += (double)Ultra_duni();
  95.                 break;
  96.             case 13:
  97.                 total += (double)Ultra_dvni();
  98.                 break;
  99.             case 14:
  100.                 mean = Ultra_vni();
  101.                 sigma = Ultra_uni();
  102.                 total += (double)Ultra_norm(mean,sigma);
  103.                 break;
  104.             case 15:
  105.                 total += (double)Ultra_expo(Ultra_uni());
  106.                 break;
  107.         }
  108.     }
  109.     fprintf(fpout,"Result after run #%d: %21.18le\n",run,total);
  110.     fprintf(fpout,"call array -->\n");
  111.     for (k=0;k<16;k++)
  112.         fprintf(fpout," %6ld",calls[k]);
  113.     fprintf(fpout,"\n\n");
  114. }
  115.  
  116. main()
  117. {
  118.     FILE    *fp;
  119.         
  120.     Ultra_seed1 = 12345678; Ultra_seed2 = 87654321;
  121. /* or use something like the following for variable input -->
  122.     printf("Input two seeds: "); scanf("%lu %lu",&Ultra_seed1,&Ultra_seed2); */
  123.     fpout = fopen("UltraDemo.out","w");
  124.     Ultra_Init();                /* Critical! */
  125.     OneSimpleTest();
  126.     printf("Simple Test Complete\n");
  127.     Ultra_RecallStart();        /* saved by Ultra_Init() */
  128.     fp = fopen("UltraTemp.dat","w");
  129.     fwrite((void *)Ultra_Remember,UltraSize,UltraLength,fp); /* there */
  130.     fclose(fp);
  131.     Exercise(1);
  132.     printf("Exercise 1 Complete\n");
  133.     fp = fopen("UltraTemp.dat","r");
  134.     fread((void *)Ultra_Remember,UltraSize,UltraLength,fp);
  135.     fclose(fp);
  136.     Ultra_RecallStart();                                     /* and back again */
  137.     Exercise(2);
  138.     printf("Exercise 2 Complete\n");
  139.     fclose(fpout);
  140. }
  141.